/**
 * Created by Alex on 16/12/31.
 */

'use strict'

const mongoose = require('mongoose')
const Restaurant = mongoose.model('Restaurant')
const Interesting = mongoose.model('Interesting')
const {imgUrl, hotUrl, imgProcessType, transConfig} = require('../../config')
const tjs = require('translation.js')

exports.findOne = function* (next) {
  let {id, type} = this.query
  if (!id || !type) {
    this.body = {
      msg: 'id or type is require',
      data: [],
      code: 201
    }
    return
  }
  let data = null
  if (type === '1') {
    data = yield Interesting
      .findOne({_id: id}, {
        content: '1',
        mainPic: '1',
        restaurantInfo: '1'
      })


    for (let i = 0, l = data.content.length; i < l; i++) {
      let item = data.content[i]
      if (item.tag === 'p' || item.tag === 'figure') {
        let x = yield tjs
          .translate({
            text: item.text,
            ...transConfig
          })
        data.content[i] = {
          ...item,
          text: x.result[0]
        }
      } else if (item.tag = 'div') {
        if (!item.imgs) continue
        for (let j = 0, len = item.imgs.length; j < len; j++) {

          let img = item.imgs[j]
          let x2 = yield tjs
            .translate({
              text: img.text,
              ...transConfig
            })
          img.text = x2.result[0]
        }
      }
    }

    let restaurantData = yield Restaurant
      .findOne({url: 'https://www.wongnai.com/' + data.restaurantInfo.url}, {
        nameCn: '1',
        extraInfo: '1',
        position: '1',
        tel: '1',
        baseInfo: '1'
      })

    if(restaurantData.baseInfo) {
      restaurantData.position.name = `${restaurantData.baseInfo.address.streetAddress}(${restaurantData.baseInfo.address.addressLocality})`
    }
    let nameCn = yield tjs
      .translate({
        text: restaurantData.nameCn,
        ...transConfig
      })
    let extraInfoGen = yield tjs
      .translate({
        text: restaurantData.extraInfo.extraInfoGen.time,
        ...transConfig
      })
    restaurantData.nameCn = nameCn.result[0]
    restaurantData.extraInfo.extraInfoGen.time = extraInfoGen.result[0]


    data._doc['restaurantData'] = restaurantData
  } else if (type === '2') {
    data = yield Restaurant
      .findOne({_id: id}, {})

    if(data.baseInfo) {
      data.position.name = `${data.baseInfo.address.streetAddress}(${data.baseInfo.address.addressLocality})`
    }

    let nameCn = yield tjs
      .translate({
        text: data.nameCn,
        ...transConfig
      })
    let extraInfoGen = yield tjs
      .translate({
        text: data.extraInfo.extraInfoGen.time,
        ...transConfig
      })

    let darenData = data.comments.find(item => item.menu.length > 0)
    let darenDataTrans = yield tjs
      .translate({
        text: darenData.content,
        ...transConfig
      })
    data._doc['darenData'] = {
      ...darenData,
      content: darenDataTrans.result[0]
    }

    for (let i = 0, l = data.menuRecommend.length; i < l; i++) {
      let item = data.menuRecommend[i]
      let trans = yield tjs.translate({
        text: item.nameCn,
        ...transConfig
      })
      item.nameCn = trans.result[0]
    }

    data.nameCn = nameCn.result[0]
    data.extraInfo.extraInfoGen.time = extraInfoGen.result[0]

  } else {
    this.body = {
      msg: 'type is not exist',
      data: [],
      code: 201
    }
    return
  }
  this.body = {
    msg: 'success',
    data: data,
    code: 200
  }
}